package com.example.main.rottweiljson.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonCommonFieldFormat {

    private List<String> paramterName = new ArrayList<>();
    private List<JsonFieldDetail> jsonFieldDetailList = new ArrayList<>();
    private String templeUrl;
    private String dateFormat;

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }

    public void setTempleUrl(String templeUrl) {
        this.templeUrl = templeUrl;
    }

    public List<String> getParamterName() {
        return paramterName;
    }

    public List<JsonFieldDetail> getJsonFieldDetailList() {
        return jsonFieldDetailList;
    }

    /**
     * 获取Map参数
     *
     * @return
     */
    public Map<String, ParamValueBean> createMapParamter() {
        Map<String, ParamValueBean> paramMap = new HashMap<>();
        for (int i = 0; i < paramterName.size(); i++) {
            ParamValueBean paramValueBean = new ParamValueBean();

            String fullParamName = paramterName.get(i);
            paramValueBean.setFullParamName(fullParamName);

            String type = fullParamName.substring(fullParamName.indexOf(":") + 1, fullParamName.indexOf("}"));
            paramValueBean.setType(type);

            String paramName = fullParamName.substring(fullParamName.indexOf("{") + 1, fullParamName.indexOf(":"));
            paramValueBean.setParamName(paramName);

            paramMap.put(fullParamName, paramValueBean);
        }
        return paramMap;
    }

    /**
     * 获取组合后的地址
     *
     * @param paramValueBeanMap
     * @return
     */
    public String getComposeUrl(Map<String, ParamValueBean> paramValueBeanMap) {
        Iterator<String> iterator = paramValueBeanMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            ParamValueBean paramValueBean = paramValueBeanMap.get(key);
            String param = paramValueBean.getValue();
            if (param == null) {
                param = "";
            }
            templeUrl = templeUrl.replace(key, param);
        }
        return templeUrl;
    }

}
